package com.skyworthclub.competition.utils.web;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.Map;

/**
 * @author li
 * @version 1.0
 * @since 18-10-18 下午11:10
 * 数据校验工具
 **/
public class DataValidateUtil {
    public static ResponseEntity<Map<String, Object>> validate(Errors errors) {

        if (errors.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> errorsMap = new HashMap<>();
            //搜集错误数据
            for (FieldError error : errors.getFieldErrors()) {
                errorsMap.put(error.getField(), error.getDefaultMessage());
            }
            map.put("status", 400);
            map.put("message", "数据非法！");

            map.put("errors", errorsMap);
            return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);
        }
        return null;
    }
}
